using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//后台用户登录
namespace ApiProject.Api.Controllers
{

    //管理员登录并获取token
    [ApiController]
    [Route("[controller]")]
    public class LoginController : ControllerBase
    {


        private IRepository<Users> _usersRepository;
        private IRepository<UserInfo> _userinfoRepository;
        private IRepository<Roles> _roleRepository;


        private Token _tokenParameter;

        private readonly IConfiguration _configuration;

        public LoginController(
            IConfiguration configuration,

            IRepository<Users> usersRepository,
            IRepository<Roles> roleRepository,
            IRepository<UserInfo> userinfoRepository


        )
        {

            _configuration = configuration;
            _tokenParameter =
                _configuration.GetSection("TokenParameter").Get<Token>();
            _usersRepository = usersRepository;
            _userinfoRepository = userinfoRepository;

            _roleRepository = roleRepository;

        }

        //管理员登录并获取token
        // Post:/login/admin
        [AllowAnonymous]
        [HttpPost, Route("admin")]
        public dynamic AdminLogin(NewUser model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            var admin = _usersRepository.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault();
            var adminId = admin.Id;
            var role = _roleRepository.Table.Where(x => x.Id == admin.RoleId).FirstOrDefault();
            var roleName = role.RoleName;



            //查找不到
            if (admin == null)
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "用户名或密码错误,请重新输入"
                    });
            }
            else
            {
                if (roleName == "管理员" || roleName == "供应商")
                {
                    //判断管理员是否删除
                    if (admin.IsDelete == false)
                    {
                        //判断管理员是否启用
                        if (admin.IsEnable == true)
                        {
                            var token = TokenHelper.GetToken(_tokenParameter, admin);
                            var refreshToken = "123456";
                            var admininfo = _userinfoRepository.GetById(admin.Id);


                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 200,
                                    Data =
                                        new
                                        {
                                            Token = token,
                                            RefreshToken = refreshToken,
                                            AdminName = username,
                                            Id = admin.Id,
                                            RealName = admininfo.NickName
                                        },
                                    Msg = "管理员登录成功，获取token成功"
                                });
                        }
                        else
                        {
                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 1000,
                                    Data = "",
                                    Msg = "该管理员已被禁用，请联系超级管理员"
                                });
                        }
                    }
                    else
                    {
                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 1000,
                                Data = "",
                                Msg = "该管理员已被删除，请联系超级管理员"
                            });
                    }
                }
                else
                {
                    return JsonHelper
                         .Serialize(new
                         {
                             Code = 1000,
                             Data = "",
                             Msg = "账号或密码错误"
                         });
                }

            }
        }
    }
}
